from Object3D import *
from OpenGL.GL import *
from OpenGL.GLU import *
from Texture import *

class Table(Object3D):

	def __init__(self):
		super(Table, self).__init__()
		self.width = 1.525
		self.height = 0.76
		self.depth = 2.74
		self.coverHeight = 0.07
		self.netHeight = 0.1525
		self.texture = Texture()
		self.tableTextureId = self.texture.loadTextures("mesa.bmp")
		self.netTextureId = self.texture.loadTextures("rede.bmp")
		self.cgTextureId = self.texture.loadTextures("cg.bmp")

	def draw(self):
		halfW = self.width / 2.0
		halfD = self.depth / 2.0

		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.1,0.1,0.2,1.0])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.4,0.4,0.9,1.0])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.8,0.8,0.8,1.0])
		glMaterialf(GL_FRONT, GL_SHININESS, 1.0)

		glPushMatrix()

		glTranslatef(*self.position.list())

		glEnable(GL_TEXTURE_2D)
		self.texture.bindTexture(self.tableTextureId)

		glBegin(GL_QUADS)
		#top face
		glNormal3f(0.0,1.0,0.0)
		glTexCoord2f(0.0, 0.0)
		glVertex3f(-halfW,0,-halfD)
		glTexCoord2f(0.0, 1.0)
		glVertex3f(-halfW,0,halfD)
		glTexCoord2f(1.0, 1.0)
		glVertex3f(halfW,0,halfD)
		glTexCoord2f(1.0, 0.0)
		glVertex3f(halfW,0,-halfD)
		glEnd()
		glDisable(GL_TEXTURE_2D)


		glEnable(GL_TEXTURE_2D)
		self.texture.bindTexture(self.cgTextureId)
		glBegin(GL_QUADS)
		#front face
		glNormal3f(0.0,0.0,1.0)
		glTexCoord2f(1.0, 1.0)
		glVertex3f(-halfW,0,-halfD)
		glTexCoord2f(0.0, 1.0)
		glVertex3f(halfW,0,-halfD)
		glTexCoord2f(0.0, 0.0)
		glVertex3f(halfW,-self.coverHeight,-halfD)
		glTexCoord2f(1.0, 0.0)
		glVertex3f(-halfW,-self.coverHeight,-halfD)
		glEnd()
		glDisable(GL_TEXTURE_2D)

		glBegin(GL_QUADS)
		#left face
		glNormal3f(1.0,0.0,0.0)
		glVertex3f(halfW,0,-halfD)
		glVertex3f(halfW,0,halfD)
		glVertex3f(halfW,-self.coverHeight,halfD)
		glVertex3f(halfW,-self.coverHeight,-halfD)
		glEnd()

		glBegin(GL_QUADS)
		#right face
		glNormal3f(-1.0,0.0,0.0)
		glVertex3f(-halfW,0,halfD)
		glVertex3f(-halfW,0,-halfD)
		glVertex3f(-halfW,-self.coverHeight,-halfD)
		glVertex3f(-halfW,-self.coverHeight,halfD)

		glEnd()

		#right legs
		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height,-0.01,-halfD*0.8,-halfD*0.77)
		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height*0.55,-self.height*0.5,-halfD*0.77,-halfD*0.23)
		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height,-0.01,-halfD*0.23,-halfD*0.2)

		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height,-0.01,halfD*0.77,halfD*0.8)
		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height*0.55,-self.height*0.5,halfD*0.23,halfD*0.77)
		self.drawCube(-halfW*0.9,-halfW*0.87,-self.height,-0.01,halfD*0.2,halfD*0.23)
		#left legs
		self.drawCube(halfW*0.87,halfW*0.9,-self.height,-0.01,-halfD*0.8,-halfD*0.77)
		self.drawCube(halfW*0.87,halfW*0.9,-self.height*0.55,-self.height*0.5,-halfD*0.77,-halfD*0.23)
		self.drawCube(halfW*0.87,halfW*0.9,-self.height,-0.01,-halfD*0.23,-halfD*0.2)

		self.drawCube(halfW*0.87,halfW*0.9,-self.height,-0.01,halfD*0.77,halfD*0.8)
		self.drawCube(halfW*0.87,halfW*0.9,-self.height*0.55,-self.height*0.5,halfD*0.23,halfD*0.77)
		self.drawCube(halfW*0.87,halfW*0.9,-self.height,-0.01,halfD*0.2,halfD*0.23)

		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.1,0.3,0.1,0.5])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.0,0.5,0.0,0.5])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.8,0.8,0.8,0.5])

		glEnable(GL_TEXTURE_2D)
		self.texture.bindTexture(self.netTextureId)
		glBegin(GL_QUADS)
		#net
		glNormal3f(0.0,0.0,-1.0)
		glTexCoord2f(0.0, 0.0)
		glVertex3f(halfW,0,0)
		glTexCoord2f(1.0, 0.0)
		glVertex3f(-halfW,0,0)
		glTexCoord2f(1.0, 1.0)
		glVertex3f(-halfW,self.netHeight,0)
		glTexCoord2f(0.0, 1.0)
		glVertex3f(halfW,self.netHeight,0)
		glEnd()

		glDisable(GL_TEXTURE_2D)

		glPopMatrix()

	def drawCube(self, xMin, xMax, yMin, yMax, zMin, zMax):
		glBegin(GL_QUADS)

		#top face
		glNormal3f(0.0,1.0,0.0)
		glVertex3f(xMin,yMax,zMin)
		glVertex3f(xMin,yMax,zMax)
		glVertex3f(xMax,yMax,zMax)
		glVertex3f(xMax,yMax,zMin)

		#front face
		glNormal3f(0.0,0.0,-1.0)
		glVertex3f(xMax,yMax,zMin)
		glVertex3f(xMax,yMin,zMin)
		glVertex3f(xMin,yMin,zMin)
		glVertex3f(xMin,yMax,zMin)

		#left face
		glNormal3f(1.0,0.0,0.0)
		glVertex3f(xMax,yMax,zMax)
		glVertex3f(xMax,yMin,zMax)
		glVertex3f(xMax,yMin,zMin)
		glVertex3f(xMax,yMax,zMin)

		#right face
		glNormal3f(-1.0,0.0,0.0)
		glVertex3f(xMin,yMax,zMin)
		glVertex3f(xMin,yMin,zMin)
		glVertex3f(xMin,yMin,zMax)
		glVertex3f(xMin,yMax,zMax)

		glEnd()